package com.example.reflectdemo.plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 分发处理器<br />
 * 将请求挨个转发给目标对象
 */
public class DispatcherInvocationHandler implements InvocationHandler {
    // 目标对象集合
    private final List<Object> targets;

    public DispatcherInvocationHandler(List<Object> targets) {
        this.targets = targets;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        for (Object target : targets){
            // 将请求转发给目标对象
            method.invoke(target, args);
        }
        return null;
    }
}
